package ca.camosun.inventory.gui;

import java.util.List;

import net.sourceforge.zbar.Symbol;
import android.app.ActionBar;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
import ca.camosun.inventory.R;
import ca.camosun.inventory.database.BoxAccessor;

import com.dm.zbar.android.scanner.ZBarConstants;
import com.dm.zbar.android.scanner.ZBarScannerActivity;

public class MainActivity extends ListActivity {

	private ArrayAdapter<String> listAdapter;
	List<String> allBoxes;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		
		setupListAdapter();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_main, menu);
		inflater.inflate(R.menu.action_bar_menu, menu);
		
		SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
		SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
		searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
		return true;
	}
	
	@Override 
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.action_search) {
			Toast.makeText(this, "Action search selected", Toast.LENGTH_SHORT).show();
		} else if (item.getItemId() == R.id.add) {
			Intent intent = new Intent(this, AddBoxActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		} else if (item.getItemId() == android.R.id.home) {
			Intent upIntent = new Intent (this, DashboardActivity.class);
			upIntent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(upIntent);
			finish();
			return true;
		}	
			return super.onOptionsItemSelected(item);
		}
	
	
	@Override
	public void onResume() {
		super.onResume();

		setupListAdapter();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	protected void OnClickListItem(int position) {
		Intent toBoxList = new Intent(this, ListItemsInOneBoxActivity.class);
		String boxName = (String) listAdapter.getItem(position);
		toBoxList.putExtra("boxName", boxName);

		startActivity(toBoxList);

	}

	public void addBox(View view) {

		Intent addBox = new Intent(this, AddBoxActivity.class);

		startActivity(addBox);

	}

	public void searchBoxQR(View view) {

		Intent intent = new Intent(this, ZBarScannerActivity.class);
		intent.putExtra(ZBarConstants.SCAN_MODES, new int[] { Symbol.QRCODE });
		startActivityForResult(intent, ZBarConstants.ZBAR_QR_SCANNER_REQUEST);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			String boxCode = data.getStringExtra(ZBarConstants.SCAN_RESULT);
			Intent toBoxList = new Intent(this, ListItemsInOneBoxActivity.class);
			BoxAccessor boxDatabase = new BoxAccessor(this);
			boxDatabase.open();
			String boxName = boxDatabase.retrieveBoxByCode(boxCode);
			boxDatabase.close();
			toBoxList.putExtra("boxName", boxName);

			startActivity(toBoxList);
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);

		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
		String selectedItem = ((TextView) info.targetView).getText().toString();

		menu.setHeaderTitle(selectedItem);

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_boxes, menu);

	}

	@Override
	public boolean onContextItemSelected(MenuItem menuItem) {
		if (menuItem.getItemId() == R.id.delete_box_button) {
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuItem
					.getMenuInfo();
			BoxAccessor boxDatabase = new BoxAccessor(this);
			boxDatabase.open();
			String boxName = allBoxes.get(info.position);
			boxDatabase.removeBox(boxName);
			allBoxes = boxDatabase.retrieveBoxes();
			boxDatabase.close();

			runOnUiThread(refreshView);
			return true;
		}

		return false;

	}

	public void setupListAdapter() {

		BoxAccessor boxDatabase = new BoxAccessor(this);
		boxDatabase.open();
		allBoxes = boxDatabase.retrieveBoxes();
		boxDatabase.close();

		if (allBoxes.isEmpty()) {
			return;
		}

		if (listAdapter == null) {

			listAdapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, allBoxes);
			setListAdapter(listAdapter);

			final ListView lv = getListView();

			lv.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					OnClickListItem(position);

				}
			});

			registerForContextMenu(lv);
			return;
		}

		runOnUiThread(refreshView);

	}

	Runnable refreshView = new Runnable() {
		public void run() {
			listAdapter.clear();
			listAdapter.addAll(allBoxes);
			listAdapter.notifyDataSetChanged();

		}

	};

}
